package network

import (
	"log"
)

const (
	E_SES_ACCEPTED = iota
	E_SES_CLOSED
	E_SES_CONNECTED
	E_SES_DISCONNECTED
)

type SesEvent struct {
	*Package
	Ses Session
}

type PeerEvent struct {
	MsgID int
	P     Peer
}

func NewSesEvent(msgId uint32, s Session, data []byte) *SesEvent {
	return &SesEvent{
		Package: &Package{MsgID: msgId, Data: data},
		Ses:     s,
	}
}

func RegistSesMsg(eq EventQueue, msgId uint32, userHandler func(interface{}, Session)) {
	eq.RegisterCallback(msgId, func(data interface{}) {
		if ev, ok := data.(*SesEvent); ok {
			userHandler(ev.Package, ev.Ses)
		} else {
			log.Println("regist ses msg not call back.")
		}
	})
}

func RegistSesMsgByID(eq EventQueue, msgId uint32, userHandler func(interface{}, Session, uint32)) {
	eq.RegisterCallback(msgId, func(data interface{}) {
		if ev, ok := data.(*SesEvent); ok {
			userHandler(ev.Package, ev.Ses, msgId)
		} else {
			log.Println("regist ses msg not call back.")
		}
	})
}
